package com.smile.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smile.common.constant.MemberLeverlConstant;
import com.smile.common.utils.PageUtils;
import com.smile.common.utils.Query;
import com.smile.member.dao.MemberDao;
import com.smile.member.entity.MemberEntity;
import com.smile.member.entity.MemberLevelEntity;
import com.smile.member.exception.PhoneExitException;
import com.smile.member.exception.UserExitException;
import com.smile.member.service.MemberService;
import com.smile.member.vo.MemberLoginVo;
import com.smile.member.vo.MemberRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {
    @Autowired
    MemberDao memberDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void registerUser(MemberRegisterVo vo) {
        MemberEntity memberEntity = new MemberEntity();
        //会员等级 1普通会员
        String memberLevel1 = MemberLeverlConstant.MEMBER_LEVEL1;
        MemberLevelEntity memberLevel = memberDao.findUserLevelById(memberLevel1);
        memberEntity.setLevelId(memberLevel.getDefaultStatus().longValue());
        
        checkNameUnique(vo.getUserName());
        checkPhoneUnique(vo.getPhone());
        //用户名 手机号
        memberEntity.setUsername(vo.getUserName());
        memberEntity.setMobile(vo.getPhone());
        //密码加密加颜处理
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(vo.getPassWord());
        memberEntity.setPassword(encode);

        baseMapper.insert(memberEntity);
    }
    @Override
    public void checkPhoneUnique(String phone) throws PhoneExitException {
        MemberDao memberDao  = this.baseMapper;
        Integer count = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if(count>0) {
            throw new PhoneExitException();
        }
    }
    @Override
    public void checkNameUnique(String userName) throws UserExitException {
        MemberDao memberDao  = this.baseMapper;
        Integer count = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("username", userName));
        if(count>0) {
            throw new PhoneExitException();
        }
        
    }

    @Override
    public MemberEntity loginUser(MemberLoginVo vo) {
        MemberDao memberDao = this.baseMapper;
        MemberEntity memberEntity = memberDao.selectOne(new QueryWrapper<MemberEntity>()
                .eq("username", vo.getLoginacct()).or().eq("mobile", vo.getLoginacct()));

        if(null!=memberEntity) {
            String password = memberEntity.getPassword();
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            boolean matches = passwordEncoder.matches(vo.getPassword(), password);
            if(matches){
                return memberEntity;
            }else{
                return null;
            }

        }else {
            return null;
        }
    }
}